package com.xjgzinfo.usdp.service;

import com.xjgzinfo.usdp.domain.BladeVisualMap;
import com.xjgzinfo.usdp.domain.BladeVisualMap_;
import com.xjgzinfo.usdp.jpa.service.QueryService;
import com.xjgzinfo.usdp.repository.BladeVisualMapRepository;
import com.xjgzinfo.usdp.service.dto.BladeVisualMapCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualMapDTO;
import com.xjgzinfo.usdp.service.mapper.BladeVisualMapMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service for executing complex queries for {@link BladeVisualMap} entities in the database.
 * The main input is a {@link BladeVisualMapCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link BladeVisualMapDTO} or a {@link Page} of {@link BladeVisualMapDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class BladeVisualMapQueryService extends QueryService<BladeVisualMap> {

    private final Logger log = LoggerFactory.getLogger(BladeVisualMapQueryService.class);

    private final BladeVisualMapRepository bladeVisualMapRepository;

    private final BladeVisualMapMapper bladeVisualMapMapper;

    public BladeVisualMapQueryService(BladeVisualMapRepository bladeVisualMapRepository, BladeVisualMapMapper bladeVisualMapMapper) {
        this.bladeVisualMapRepository = bladeVisualMapRepository;
        this.bladeVisualMapMapper = bladeVisualMapMapper;
    }

    /**
     * Return a {@link List} of {@link BladeVisualMapDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<BladeVisualMapDTO> findByCriteria(BladeVisualMapCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<BladeVisualMap> specification = createSpecification(criteria);
        return bladeVisualMapMapper.toDto(bladeVisualMapRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link BladeVisualMapDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page     The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<BladeVisualMapDTO> findByCriteria(BladeVisualMapCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<BladeVisualMap> specification = createSpecification(criteria);
        return bladeVisualMapRepository.findAll(specification, page)
                .map(bladeVisualMapMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(BladeVisualMapCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<BladeVisualMap> specification = createSpecification(criteria);
        return bladeVisualMapRepository.count(specification);
    }

    /**
     * Function to convert {@link BladeVisualMapCriteria} to a {@link Specification}
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<BladeVisualMap> createSpecification(BladeVisualMapCriteria criteria) {
        Specification<BladeVisualMap> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), BladeVisualMap_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), BladeVisualMap_.name));
            }
        }
        return specification;
    }
}